package data.set.adaptator;

import java.util.ArrayList;
import java.util.Iterator;

import tools.Pair;

import data.set.OfflineSet;
import data.set.OfflineSetIterator;


public class PairOfflineSet_To_BinaryClassificationOfflineSet_OneAgainstOther<X,Y> implements OfflineSet<Pair<X,Boolean>>
{
	private OfflineSet<Pair<X,Y>> set;
	private ArrayList<Integer> ids;
	private Y positive_class;
	private Y negative_class;
	
	public PairOfflineSet_To_BinaryClassificationOfflineSet_OneAgainstOther(OfflineSet<Pair<X,Y>> set,Y positive_class,Y negative_class)
	{
		this.set=set;
		this.ids=new ArrayList<Integer>();
		this.positive_class=positive_class;
		this.negative_class=negative_class;

		
		OfflineSetIterator<Pair<X,Y>> it=set.inputsetiterator();
		while(it.hasNext())
		{
			it.next();
			if ((it.getCurrentObject().getY().equals(positive_class)) || (it.getCurrentObject().getY().equals(negative_class)))
					ids.add(it.getCurrentId());
		}
	}
	
	
	public Pair<X, Boolean> getObject(int id) {
		Pair<X,Y> v=set.getObject(id);
		Y c=v.getY();
		if (c.equals(positive_class))
			return new Pair<X,Boolean>(v.getX(),true);
		if (c.equals(negative_class))
			return new Pair<X,Boolean>(v.getX(),false);
		return(null);
	}
	
	
	public OfflineSetIterator<Pair<X, Boolean>> inputsetiterator() {
		return(new IT(set.inputsetiterator()));
	}
	
	public long size() {
		return(ids.size());
	}
	
	public Iterator<Pair<X, Boolean>> iterator() {
		return(new Iterator_FromOfflineSetIterator<Pair<X,Boolean>>(inputsetiterator()));
	}
	
	private class IT implements OfflineSetIterator<Pair<X,Boolean>>
	{
		private int current_index;
		
		public IT(OfflineSetIterator<Pair<X,Y>> it)
		{
			this.current_index=-1;
		}

		public int getCurrentId() {
			return(ids.get(current_index));
		}

		
		public Pair<X, Boolean> getCurrentObject() {
			int id=ids.get(current_index);
			Pair<X,Y> v=set.getObject(id);
			Y c=v.getY();
			if (c.equals(positive_class))
				return new Pair<X,Boolean>(v.getX(),true);
			if (c.equals(negative_class))
				return new Pair<X,Boolean>(v.getX(),false);
			return(null);
		}

		
		public boolean hasNext() {
			return(current_index<ids.size()-1);
		}

		
		public void next() {
			current_index++;
		}
	}
}
